<template>
    <div class="login-body">
        <div class="login-card">
            <div class="left-box">
                <!-- <div class="title">中保驰</div>
                <div class="desc">欢迎来到中保驰后台管理系统</div> -->
                <img src="@/assets/images/login/peitu.png" class="img" />
            </div>
            <div class="right-box">
                <div class="title">登录</div>
                <el-form ref="form" :model="formData" class="login-form" :rules="rules" size="medium" @submit.native.prevent="doLogin()">
                    <el-form-item prop="account">
                        <el-input v-model="formData.account" prefix-icon="el-icon-user-solid" placeholder="请输入账号" clearable></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="formData.password" prefix-icon="el-icon-lock" placeholder="请输入密码" clearable show-password></el-input>
                    </el-form-item>
                    <el-form-item prop="captcha">
                        <div class="captcha-box">
                            <el-input v-model="formData.captcha" placeholder="请输入验证码" clearable class="mr5" @keyup.enter.native="doLogin()"></el-input>
                            <img :src="captcha" slot="suffix" class="captcha-img" @click="refreshCaptcha" />
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <div class="login-btn" @click="doLogin()">登录</div>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
import api from "@/api";
import { refresh } from "less";
export default {
    name: "login",
    data() {
        return {
            captcha: "",
            formData: {
                account: "",
                password: "",
                captcha: "",
            },
            rules: {
                account: [
                    {
                        required: true,
                        message: "请输入账号",
                        trigger: "blur",
                    },
                ],
                password: [
                    {
                        required: true,
                        message: "请输入密码",
                        trigger: "blur",
                    },
                ],
                captcha: [
                    {
                        required: true,
                        message: "请输入验证码",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    created() {
        this.refreshCaptcha();
    },
    methods: {
        refreshCaptcha() {
            this.captcha = api.getCaptcha() + "?random=" + Math.random();
        },
        doLogin() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    api.doLogin(this.formData)
                        .then((res) => {
                            this.$Message.success({
                                content: "登录成功",
                                duration: 1,
                            });
                            this.$store.dispatch("updateUserInfo", res);
                            this.goHome();
                        })
                        .catch(() => {
                            this.refreshCaptcha();
                            this.formData.captcha = "";
                            this.$refs.form.clearValidate("captcha");
                        });
                }
            });
        },
    },
};
</script>
<style scoped lang="less">
.login-body {
    width: 100vw;
    height: 100vh;
    background-color: #f3f5f7;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;

    .login-card {
        width: 800px;
        background: #fff;
        border-radius: 20px;
        box-shadow: 0 0 6px 0 rgba(0, 70, 130, 0.14);
        display: flex;
        align-items: center;
        justify-content: center;

        .left-box {
            flex: 1;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 30px;
            user-select: none;

            .title {
                font-weight: 600;
                color: #e94445;
                font-size: 32px;
            }

            .desc {
                color: #8c95b0;
                font-size: 14px;
            }

            .img {
                width: 100%;
            }
        }

        .right-box {
            flex: 1;
            height: 100%;
            padding: 30px;
            margin: 30px;
            box-sizing: border-box;
            background: #fff;
            border-radius: 20px;

            .title {
                font-size: 20px;
            }

            .login-form {
                margin-top: 30px;
                /deep/ .el-input__inner {
                    height: 50px;
                }
                .login-btn {
                    width: 100%;
                    color: #fff;
                    background-color: #e94445;
                    border-color: #e94445;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    cursor: pointer;
                    border-radius: 3px;
                    user-select: none;
                    border-radius: 8px;
                }

                .captcha-box {
                    display: flex;
                    align-items: center;

                    .captcha-img {
                        // padding: 1px;
                        width: 110px;
                        height: 50px;
                        border-radius: 5px;
                        cursor: pointer;
                    }
                }
            }
        }
    }
}
</style>
